package com.example.mallsystem1104.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.mallsystem1104.annotation.Authority;
import com.example.mallsystem1104.common.Result;
import com.example.mallsystem1104.constants.Constants;
import com.example.mallsystem1104.entity.AuthorityType;
import com.example.mallsystem1104.entity.Carousel;
import com.example.mallsystem1104.entity.Good;
import com.example.mallsystem1104.exception.ServiceException;
import com.example.mallsystem1104.service.CarouselService;
import com.example.mallsystem1104.service.GoodService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author zy
 * @Date 2024/12/11 14:28
 * @packageName:com.example.mallsystem1104.controller
 * @ClassName:CarouselController
 */

@RestController
@RequestMapping("/api/carousel")
@CrossOrigin
public class CarouselController {

    @Resource
    private CarouselService carouselService;

    @Resource
    private GoodService goodService;

    @GetMapping
    public Result findAll(){
        List<Carousel> carouselList=carouselService.getAllCarousel();
        return Result.success(carouselList);
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable int id){
        /*Carousel carousel=carouselService.getOne(Wrappers.<Carousel>lambdaQuery().eq(Carousel::getId,id));
        return Result.success(carousel);*/
        return Result.success(carouselService.getById(id));
    }

    @PostMapping
    @Authority(AuthorityType.requireAuthority)
    public Result save(@RequestBody Carousel carousel){
        Good good=goodService.getById(carousel.getGoodId());
        if(good==null){
            throw new ServiceException("400","商品"+carousel.getGoodId()+"不存在");
        }
        carouselService.saveOrUpdate(carousel);
        return Result.success();
    }

    @PutMapping
    @Authority(AuthorityType.requireAuthority)
    public Result update(@RequestBody Carousel carousel){
        Good good=goodService.getById(carousel.getGoodId());
        if(good==null){
            throw new ServiceException("400","商品"+carousel.getGoodId()+"不存在");
        }
        carouselService.updateById(carousel);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    @Authority(AuthorityType.requireAuthority)
    public Result deleteById(@PathVariable int id){
        carouselService.removeById(id);
        return Result.success();
    }
}
